import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class StopWatchGui extends JPanel{
	
	private static JTextField display = new JTextField(12);
	static StopWatchFunctionality watch = new StopWatchFunctionality();
	
	public StopWatchGui(){
		//frame stuff
//		setTitle("Stop Watch");
		setLayout(new BorderLayout());
//	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//	    setSize(500, 500);
//	    setLocationRelativeTo(null); // Center the frame
//	    setVisible(true);
//	    setResizable(false);
	    
	    //display stuff
	    Font font = new Font("Lucida Console", Font.PLAIN, 54);
	    
	    display.setFont(font);
	    display.setText("00:00:00:000");
	    display.setHorizontalAlignment(JTextField.CENTER);
	    display.setEditable(false);
	    add(display, BorderLayout.CENTER);
	    	    
	    
	    //Buttons
	    final JButton startButton = new JButton("Start");
	    final JButton stopButton = new JButton("Stop");
	    final JButton resetButton = new JButton("Reset");
	    
	    //Button Listeners
	    startButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				watch.start();
				updateDisplay();
				resetButton.setEnabled(false);
				startButton.setEnabled(false);
				stopButton.setEnabled(true);
			}});
	    
	    stopButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				watch.stop();
				updateDisplay();
				resetButton.setEnabled(true);
				stopButton.setEnabled(false);
				startButton.setEnabled(true);
			}});
	    
	    resetButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
					watch.reset();
					updateDisplay();
			}});
	    
	    
	    //Button panel
	    JPanel buttonP = new JPanel();
	    buttonP.setLayout(new GridLayout(0,3,5,0));
	    buttonP.add(startButton);
	    buttonP.add(stopButton);
	    buttonP.add(resetButton);
	    
	    //add ButtonP to frame
	    add(buttonP, BorderLayout.SOUTH);
	}//End of no-arg constructor
	
	public static void updateDisplay(){
		StringBuilder millisecondS = new StringBuilder(Integer.toString(watch.getMillisecond()));
		StringBuilder secondS = new StringBuilder(Integer.toString(watch.getSecond()));
		StringBuilder minuteS = new StringBuilder(Integer.toString(watch.getMinute()));
		StringBuilder hourS = new StringBuilder(Integer.toString(watch.getHour()));
		
		for(int i = 0; i < 3; i ++){
			if(millisecondS.length()<3){
				millisecondS=millisecondS.insert(0, "0");
			}
			if(secondS.length()<2){
				secondS = secondS.insert(0, "0");
			}
			if(minuteS.length()<2){
				minuteS = minuteS.insert(0, "0");
			}
			if(hourS.length()<2){
				hourS = hourS.insert(0, "0");
			}
		}
		
		display.setText(hourS+":"+minuteS+":"+secondS+":"+millisecondS);
	}//End of updateDisplay class
	
	

	
/*	public static void main(String[] args) {
		
		StopWatchGui stopWatch = new StopWatchGui();
		stopWatch.setVisible(true);
	}
*/
}
